<?php

namespace backend\controllers;

use app\models\ArcProgress;
use function var_dump;
use Yii;
use yii\db\Command;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use common\models\LoginForm;

/**
 * Site controller
 */
class SiteController extends CommonController {
	/**
	 *
	 * {@inheritdoc}
	 *
	 */
	public function behaviors() {
		return [ 
				'access' => [ 
						'class' => AccessControl::className (),
						'rules' => [ 
								[ 
										'actions' => [ 
												'login',
												'error'
										],
										'allow' => true 
								],
								[
										'actions' => [ 
												'logout',
												'index' 
										],
										'allow' => true,
										'roles' => [ 
												'@' 
										] 
								] 
						] 
				],
				'verbs' => [ 
						'class' => VerbFilter::className (),
						'actions' => [ 
								'logout' => [ 
										'post' 
								] 
						] 
				] 
		];
	}
	
	/**
	 *
	 * {@inheritdoc}
	 *
	 */
	public function actions() {
		return [ 
				'error' => [ 
						'class' => 'yii\web\ErrorAction' 
				] 
		];
	}
	
	/**
	 * Displays homepage.
	 *
	 * @return string
	 */
	public function actionIndex() {
	    $start = strtotime(date('Y-m-d',time()));
	    $date_start = date('Y年m月d日 H:i:s',$start);
        $end = $start + 86400;
        $date_end = date('Y年m月d日 H:i:s',$end-1);
	    $start = strtotime(date('Y-m-d',time()));
        $end = $start + 86400;
        //有问题
        $problem = ArcProgress::find()->where(['>=','create_time',$start])->andWhere(['<','create_time',$end])->andWhere(['=','arc_classify_status',1])->count();
        //没问题
        $noproblem = ArcProgress::find()->where(['>=','create_time',$start])->andWhere(['<','create_time',$end])->andWhere(['=','arc_classify_status',2])->count();
        //今日识别总量
        $today = $problem+$noproblem;
//        var_dump($problem);
//        var_dump($noproblem);exit;
        //前1天
        $problem1 = ArcProgress::find()->where(['>=','create_time',$start-86400])->andWhere(['<','create_time',$start])->andWhere(['=','arc_classify_status',1])->count();
//        echo date('Y-m-d H:i:s',strtotime(date('Y-m-d',strtotime('-2 day'))));
//        exit;
        //前2天
        $problem2 = ArcProgress::find()->where(['>=','create_time',strtotime(date('Y-m-d',strtotime('-2 day')))])->andWhere(['<','create_time',strtotime(date('Y-m-d',strtotime('-1 day')))])->andWhere(['=','arc_classify_status',1])->count();
        //前3天
        $problem3 = ArcProgress::find()->where(['>=','create_time',strtotime(date('Y-m-d',strtotime('-3 day')))])->andWhere(['<','create_time',strtotime(date('Y-m-d',strtotime('-2 day')))])->andWhere(['=','arc_classify_status',1])->count();
        //前4天
        $problem4 = ArcProgress::find()->where(['>=','create_time',strtotime(date('Y-m-d',strtotime('-4 day')))])->andWhere(['<','create_time',strtotime(date('Y-m-d',strtotime('-3 day')))])->andWhere(['=','arc_classify_status',1])->count();
        //前5天
        $problem5 = ArcProgress::find()->where(['>=','create_time',strtotime(date('Y-m-d',strtotime('-5 day')))])->andWhere(['<','create_time',strtotime(date('Y-m-d',strtotime('-4 day')))])->andWhere(['=','arc_classify_status',1])->count();
        //前6天
        $problem6 = ArcProgress::find()->where(['>=','create_time',strtotime(date('Y-m-d',strtotime('-6 day')))])->andWhere(['<','create_time',strtotime(date('Y-m-d',strtotime('-5 day')))])->andWhere(['=','arc_classify_status',1])->count();

        //没问题
        //前1天
        $noproblem1 = ArcProgress::find()->where(['>=','create_time',$start-86400])->andWhere(['<','create_time',$start])->andWhere(['=','arc_classify_status',2])->count();
        //前2天
        $noproblem2 = ArcProgress::find()->where(['>=','create_time',strtotime(date('Y-m-d',strtotime('-2 day')))])->andWhere(['<','create_time',strtotime(date('Y-m-d',strtotime('-1 day')))])->andWhere(['=','arc_classify_status',2])->count();
        //前3天
        $noproblem3 = ArcProgress::find()->where(['>=','create_time',strtotime(date('Y-m-d',strtotime('-3 day')))])->andWhere(['<','create_time',strtotime(date('Y-m-d',strtotime('-2 day')))])->andWhere(['=','arc_classify_status',2])->count();
        //前4天
        $noproblem4 = ArcProgress::find()->where(['>=','create_time',strtotime(date('Y-m-d',strtotime('-4 day')))])->andWhere(['<','create_time',strtotime(date('Y-m-d',strtotime('-3 day')))])->andWhere(['=','arc_classify_status',2])->count();
        //前5天
        $noproblem5 = ArcProgress::find()->where(['>=','create_time',strtotime(date('Y-m-d',strtotime('-5 day')))])->andWhere(['<','create_time',strtotime(date('Y-m-d',strtotime('-4 day')))])->andWhere(['=','arc_classify_status',2])->count();
        //前6天
        $noproblem6 = ArcProgress::find()->where(['>=','create_time',strtotime(date('Y-m-d',strtotime('-6 day')))])->andWhere(['<','create_time',strtotime(date('Y-m-d',strtotime('-5 day')))])->andWhere(['=','arc_classify_status',2])->count();

		return $this->render ( 'index',['today'=>$today,'problem'=>$problem,'noproblem'=>$noproblem,'problem1'=>$problem1,'problem2'=>$problem2,'problem3'=>$problem3,'problem4'=>$problem4,'problem5'=>$problem5,'problem6'=>$problem6,'noproblem1'=>$noproblem1,'noproblem2'=>$noproblem2,'noproblem3'=>$noproblem3,'noproblem4'=>$noproblem4,'noproblem5'=>$noproblem5,'noproblem6'=>$noproblem6,'date_start'=>$date_start,'date_end'=>$date_end]);
	}
	/**
	 * 注册
	 */
//	public function actionRegister() {
//		$this->layout = false;
//		return $this->render('register'); //显示注册页面
//	}
//
//	public function actionDoregister() {
//		$user= new User;
//		$post = Yii::$app->request->post();
//		$username = $post['username'];
//		$email = $post['email'];
//		$password_hash = Yii::$app->security->generatePasswordHash($post['password_hash']);
//		$result = $user->find()->where(['username'=>$username])->all();
//		if($result)
//		{
//			echo 2;
//		}elseif ($user->find()->where(['email'=>$email])->all()) {
//			echo 3;
//		}else{
//			$user->username = $post['username'];
//			$user->email = $post['email'];
//			$user->password_hash = $password_hash;
//			$user->auth_key = $post['auth_key'];
//				if ($user->save()) {
//					echo 1;
//				};
//			}
//	}
	
	/**
	 * Login action.
	 *
	 * @return string
	 */
	public function actionLogin() {
		if (! Yii::$app->user->isGuest) {
			return $this->goHome ();
		}
		
		$model = new LoginForm ();
		if ($model->load ( Yii::$app->request->post () ) && $model->login ()) {
			return $this->goBack ();
		} else {
			$model->password = '';
			
			return $this->render ( 'login', [ 
					'model' => $model 
			] );
		}
	}
	
	/**
	 * Logout action.
	 *
	 * @return string
	 */
	public function actionLogout() {
		Yii::$app->user->logout ();
		//清除cookie以防止不同胡浏览器出现兼容问题无法退出的问题
        Yii::$app->response->cookies->remove('_identity-backend');
		return $this->goHome ();
	}
}
